﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GlobalObjects;

namespace DataAccessLayer
{
    public class NotificationEntity
    {
        /// <summary>
        /// inserts a new notification record in the database
        /// </summary>
        /// <param name="userId">user id</param>
        /// <param name="deviceId">device id</param>
        /// <param name="notificationReason">reason for the notification</param>
         public void insertNotification_DAL(int userId,int deviceId,string notificationReason,int sensorId)
        {
            using (var context = new MSR2DBEntities())
            {
                var query = from n in context.notifications
                            where n.user_id == userId && n.device_id == deviceId && n.reason == notificationReason && n.sensor_id == sensorId
                            select n;
                if (query.Count() > 0)
                {
                   // DbUpdateException exc = new DbUpdateException();
                   // throw exc;
                }
                else
                {
                    var nNotification = new notification
                    {
                        user_id = userId,
                        device_id = deviceId,
                        reason = notificationReason,
                        sensor_id = sensorId
                    };
                    try
                    {
                        //Email
                        EmailSender email = new EmailSender();
                        UserEntity userEntity = new UserEntity();
                        UserDetails user = userEntity.getUserById_DAL(userId);
                        Task.Factory.StartNew(() => email.sendEmail(user.EmailAddress, "sensor id: " + sensorId + " has been triggered", "Dear " + user.FirstName + ", \n \n the " + nNotification .reason+ " of your sensor has been triggered. \n \n Regards, \n UCL Gadgeteer Team"));
                        //
                        context.notifications.Add(nNotification);
                        context.SaveChanges();
                    }
                    catch (DbUpdateException exc)
                    {
                        throw exc;
                    }
                }
            }
         }

        /// <summary>
        /// removes a notification from the database
        /// </summary>
        /// <param name="userId">user id</param>
        /// <param name="deviceId">device id</param>
         public void removeNotification_DAL(int notId)
         {
             using (var context = new MSR2DBEntities())
             {
                 var query = from n in context.notifications
                             where n.id == notId
                             select n;
                 if (query.Count() == 1)
                 {
                     context.notifications.Remove(query.First());
                     context.SaveChanges();
                 }
                 else
                 {
                     DbUpdateException exc = new DbUpdateException("error. notification id doesn't exist");
                     throw exc;
                 }
             }
         }

        /// <summary>
        /// returns a list of notification for a given user id
        /// </summary>
        /// <param name="userId">user id</param>
        /// <returns>list of notification objects</returns>
         public List<Notification> getNotifications_DAL(int userId) {
             List<Notification> list = new List<Notification>();
             using (MSR2DBEntities ctx = new MSR2DBEntities())
             {
                 var query = from n in ctx.notifications
                             where n.user_id == userId
                             select n;
                 foreach (var not in query)
                 {
                     Notification notification = new Notification();
                     notification.userId = not.user_id;
                     notification.deviceId = not.device_id;
                     notification.reason = not.reason;
                     notification.id = not.id;
                     notification.sensorId = not.sensor_id;
                     list.Add(notification);
                 }
                 return list;
             }
         }
        /// <summary>
        /// returns a list of notifications based on the given user id and the specified filter
        /// </summary>
        /// <param name="userId">user id</param>
        /// <param name="filter">filter</param>
        /// <returns>a list of notification objects</returns>
         public List<Notification> getNotificationsByFilter_DAL(int userId,string filter)
         {
             List<Notification> list = new List<Notification>();
             using (MSR2DBEntities ctx = new MSR2DBEntities())
             {
                 var query = from n in ctx.notifications
                             where n.user_id == userId && n.reason == filter
                             select n;
                 foreach (var not in query)
                 {
                     Notification notification = new Notification();
                     notification.userId = not.user_id;
                     notification.deviceId = not.device_id;
                     notification.reason = not.reason;
                     notification.id = not.id;
                     notification.sensorId = not.sensor_id;
                     list.Add(notification);
                 }
                 return list;
             }
         }
    }
}
